var DAO = require('./DAO')
var orderDAO = {
    getOrder:function(callback){
        DAO('select * from orders',function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    getCart:function(cartID,callback){
        DAO('select * from cart where cartID = ?', cartID,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    delOrder:function(orderID,callback){
        DAO('delete from orders  where orderID = ?',orderID,function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    insertOrder:function(orders,callback){
        console.log(orders)
        DAO('insert into orders(total,orderTime,Payment) values(?,now(),?)',[orders.total,orders.Payment],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    updOrder:function(order,callback){
        console.log(order)
        DAO('update `orders` set place = ?,tel = ? where orderID = ?',[order.place,order.tel,order.orderID],function(err,results){
            if(err){
                callback(err,null)
            }else{
                callback(null,results)
            }
        })
    },
    endOrder:function(orderID,callback){
        DAO('select total from `orders` where orderID = ?',orderID,function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    yesOrder:function(Payment,callback){
        DAO('select * from `orders` where Payment = 1 ',Payment,function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    YesOrder:function(orderID,callback){
        DAO('update orders set Payment = 1  where orderID = ? ',orderID,function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    noOrder:function(Payment,callback){
        DAO('select * from `orders` where Payment = 0 ',Payment,function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    selorderdet:function(callback){
        DAO('select total,orderTime,payment ,tradeName,tradeNum,price from orderdet join orders on orderdet.orderID = orders.orderID join trade on orderdet.tradeID = trade.tradeID ',null,function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    //订单详情表
    insertorderdet:function(ids,callback){
        DAO('insert into orderdet(discountID,tradeID,orderID,num,address) select discountID, tradeID,orderID,num,address from (cart join orders on cart.userId = orders.userID) join users on cart.userId = users.userId and cartID in (?) and orderID =?',[ids.cartID.ids.orderID],function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    //订单表
    addOrder:function(orders,callback){
        DAO('insert into orders(userId,orderTime,total,postaddress,postuser,posttel) values(?,now(),?,?,?)',[orders.userId,orders.total,orders.postaddress,orders.postuser,orders.posttel],function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
    //获取购物车内商品信息
    getTrade:function([userId,cartID],callback){
        DAO('SELECT cartID,sum,discount.discountID id,num,newprice newmoney,pastprice pastprice,disName cname,disimage1 cpic,discontent content FROM cart,discount where userId=? and cartID in ('+cartID+') and type=1 and cart.discountID=discount.discountID union SELECT cartID,sum,trade.tradeID id,num,price newmoney,newprice pastprice,tradeName cname,images cpic,advantage content FROM cart,trade where userId=? and cartID in ('+cartID+') and type=0 and cart.tradeID=trade.tradeID',[userId,userId],function(err,results){
            if(err){
                callback(err,null)
            }
            else{
                callback(null,results)
            }
        })
    },
}
module.exports = orderDAO